
/* JavaScript content from js/SmartTest7_17.js in folder common */
/*
 * Licensed Materials - Property of IBM
 * 5725-G92 (C) Copyright IBM Corp. 2006, 2013. All Rights Reserved.
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */


$(document).ready(function() {
	$("#phone").keydown(function() {
		var curLength = $("#phone").val().length;
		if (curLength >= 11) {
			var phone = $("#phone").val().substr(0,10);
			$("#phone").val(phone);
			alert("phone number is incorrect format!");
		}
	});
	
	$("#password").keydown(function() {
		var curLength = $("#password").val().length;
		if (curLength >= 20) {
			var phone = $("#password").val().substr(0,19);
			$("#password").val(phone);
			//alert("phone number is incorrect format!");
		}
	});
});

function wlCommonInit() {
	window.customer = {};
	window.customer.id = '32424';
	window.backDriInfo = {};
	window.backDriInfo.driverInfo = {};
	window.busy = new WL.BusyIndicator("content", {
		text : "Loading"
	});
	window.showToast = function(msg, delayTime){
		$("<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'>"+msg+"</div>")
		.css({  
			display: "block", 
			opacity: 0.80, 
			position: "fixed",
			padding: "4px 18px",
			"text-align": "center",
			top: "80%"
		})
		.appendTo( $("#page")).delay(1000)
		.fadeOut(delayTime, function(){
			$(this).remove();
		});
	};
}
(function($) {
	$("#driverBtn").click(controlRecord);
})(jQuery);

function loadFeeds() {
	var invocationData = {
		adapter : 'typAdp',
		procedure : 'getStories',
		parameters : [{
			'latitude' : '111',
			'longitude' : '333'
		}]
	};

	WL.Client.invokeProcedure(invocationData, {
		onSuccess : loadFeedsSuccess,
		onFailure : loadFeedsFailure
	});
}
function loadFeedsSuccess(result) {
	alert(result.invocationResult.result);
}
function loadFeedsFailure(result) {
	WL.Logger.error("Feed retrieve failure");
}

//-------------------------------------
var mymedia = null;
var recordSrc = "smarttaxiaaaa/record.ogg";

/**
 * after tap the mic start record or stop record
 */
function controlRecord() {
	var currentStatus = $('#driverBtn').data('state');
	if (currentStatus == "stop") {
		startRecordAudio();
		$('#driverBtn').data('state', "running");
	} else if (currentStatus == "running") {
		stopRecordAudio();
		$('#driverBtn').data('state', "stop");
	}
}

function startRecordAudio() {
	if (mymedia == null) {
		var recAudioSuccess = function() {
//			alert("startRecordAudio():Audio new Success");
		};
		var recAudioError = function(err) {
//			alert("startRecordAudio():Audio Error: " + err);
		};
		var recAudioStatus = function(status) {
//			alert("startRecordAudio():Audio Status: " + Media.MEDIA_MSG[status]);
		};
		mymedia = new Media(recordSrc, recAudioSuccess, recAudioError,
				recAudioStatus);
	}
	alert("startRecord");
	mymedia.startRecord();
}

function stopRecordAudio() {
	mymedia.stopRecord();
	playRecord();
}

function playRecord() {
	var media1 = new Media(window.recordSrc, function() {
//		alert("playAudio():Audio new Success");
	}, function(err) {
//		alert("playAudio():Audio new Error: " + err);
	}, function(status) {
//		alert("playAudio():Audio Status: " + Media.MEDIA_MSG[status]);
	});
	alert("playRecord");
	media1.play();
//	var fileLocation = "/data/data/com.SmartTest7_17/files/www/default/record.ogg";
//	alert(fileLocation);
	cordova.exec(encodeSuccess, encodeFailure, "EncodeUtilPlugin", "encodeAudio", [recordSrc]);
	
}

function encodeSuccess(result) {
	alert("success:" + result);
	var cus_id = "1";
	var dri_id = "1";
	var invocationData = {
			adapter : 'stAdp',
			procedure : 'sendAudio',
			parameters : [{'cus_id': cus_id,'dri_id':dri_id,'encodedString':result}]
	};
	WL.Client.invokeProcedure(invocationData, {
		onSuccess : receiveEncodeAudio,
		onFailure : decodeFailure
	});
}

function receiveEncodeAudio(result) {
	//alert(result.encodedString);
	var encodedContent = result.invocationResult.encodedString;
	cordova.exec(decodeSuccess, decodeFailure, "EncodeUtilPlugin", "decodeAudio", [encodedContent]);
}

function encodeFailure(result) {
	alert("fail:" + result);
}

function decodeSuccess(result) {
	alert("decodeSuccess");
}

function decodeFailure(result) {
	alert("decodeFail:");
}
/* JavaScript content from js/SmartTest7_17.js in folder android */
/*
 *  Licensed Materials - Property of IBM
 *  5725-G92 (C) Copyright IBM Corp. 2011, 2013. All Rights Reserved.
 *  US Government Users Restricted Rights - Use, duplication or
 *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

// This method is invoked after loading the main HTML and successful initialization of the Worklight runtime.
function wlEnvInit(){
    wlCommonInit();
    // Environment initialization code goes here
}